<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
defined('_JEXEC') or die();

jimport('joomla.application.component.model');

class ArticleflowModelArticleflow_edit extends JModel {

    var $_data;
    var $_id;
    var $_cmmdata;
    var $_total = null;
    var $_pagination = null;

    function __construct($config = array()) {

        parent::__construct($config);
        global $mainframe, $option;
        $array = JRequest::getVar('cid', array(), '', 'array');
        $edit = JRequest::getVar('edit', true);

        if ($edit) {

            $this->setId((int) $array[0]);
        }
        // Get the pagination request variables
        $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = $mainframe->getUserStateFromRequest($option . '.limitstart', 'limitstart', 0, 'int');

        // In case limit has been changed, adjust limitstart accordingly
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
    }

    function setId($id) {
        $this->_id = $id;
        $this->_data = null;
    }

    function getData() {
        if (!$this->_Loaddata()) {
            $this->_initData();
        }
        else
            return $this->_data;
    }

    function getCommentData() {

        if (empty($this->_cmmdata)) {
            $cm_query = $this->_buildQuery();
            $this->_cmmdata = $this->_getList($cm_query, $this->getState('limitstart'), $this->getState('limit'));
        }
        return $this->_cmmdata;
    }

    function _Loaddata() {
        if (empty($this->_data)) {
            $query = 'SELECT a.*,ss.title as section'
                    . ' FROM #__content_temp AS a'
                    . ' LEFT JOIN #__sections AS ss ON ss.id = a.sectionid'
                    . ' WHERE a.id = ' . (int) $this->_id;

            $this->_db->setQuery($query);
            $this->_data = $this->_db->loadObject();
            return (boolean) $this->_data;
        }
        return true;
    }

    function _buildQuery() {
        //echo 'article izd :  ' . (int) $this->_id;
        $user = & JFactory::getUser();
        $uid = $user->get('id');
        $cm_query = 'SELECT c.*'
                . ' FROM #__content_comment AS c'
                . ' WHERE c.user_reciever = ' . $uid . ' AND c.article_id=' . (int) $this->_id
                . ' ORDER BY c.create_date DESC' 
        ;
        //echo $cm_query;
        return $cm_query;
    }

   

    function _initData() {
        if (empty($this->_data)) {
            $article = new stdClass();
            $article->id = 0;

            $article->published = 1;
            $article->title = '';
            $article->created_by = 1;
            $article->modified_by = 1;
            $article->created = gmdate('Y-m-d H:i:s');
            //$article->modified = gmdate('Y-m-d H:i:s');
            $this->_data = $article;
            return (boolean) $this->_data;
        }
        return true;
    }

    function checkout($uid = null) {
        if ($this->_id) {
            // Make sure we have a user id to checkout the article with
            if (is_null($uid)) {
                $user = & JFactory::getUser();
                $uid = $user->get('id');
            }
            // Lets get to it and checkout the thing...
            $weblink = & $this->getTable();
            if (!$weblink->checkout($uid, $this->_id)) {
                $this->setError($this->_db->getErrorMsg());
                return false;
            }

            return true;
        }
        return false;
    }

    function store($data) {

        $row = & $this->getTable();
        //Dua cac truong trong form vao bang
        if (!$row->bind($data)) {

            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        //print_r($row);
        // Make sure the web link tale is valid
        if (!$row->check()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        // Store the web link table to the database
        if (!$row->store()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
        return true;
    }

    function storeAs($data) {

        $row = & $this->getTable('articleflow_full');
        //Dua cac truong trong form vao bang
        if (!$row->bind($data)) {

            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        //print_r($row); die();
        // Make sure the web link tale is valid
        if (!$row->check()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }


        if (!$row->store()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
        return true;
    }

    function saveContentEdit($data) {

        $row = & $this->getTable('articleflow_temp_edit');
        //Dua cac truong trong form vao bang
        if (!$row->bind($data)) {

            $this->setError($this->_db->getErrorMsg());
            return false;
        }
        //  print_r($row);
        if (!$row->check()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        if (!$row->store()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
        return true;
    }

    function delete($cid = array()) {
        $results = false;

        if (count($cid)) {
            JArrayHelper::toInteger($cid);
            $cids = implode(',', $cid);
            //echo $cids; die();
            $query = 'DELETE FROM #__content_temp WHERE id = ' . $cids;

            $this->_db->setQuery($query);

            if ($this->_db->query()) {
                $this->setError($this->_db->getErrorMsg());
                return false;
            }
        }
        return true;
    }

    function del_content_temp_edit($cid = array()) {
        $results = false;

        if (count($cid)) {
            JArrayHelper::toInteger($cid);
            $cids = implode(',', $cid);
            //echo $cids; die();
            $query = 'DELETE FROM #__content_temp_edit WHERE content_temp_id = ' . $cids;

            $this->_db->setQuery($query);

            if ($this->_db->query()) {
                $this->setError($this->_db->getErrorMsg());
                return false;
            }
        }
        return true;
    }
    
    function delete_comment_byArticleID($cid=array())
    {
         $results = false;

        if (count($cid)) {
            JArrayHelper::toInteger($cid);
            $cids = implode(',', $cid);
            //echo $cids; die();
            $query = 'DELETE FROM #__content_comment WHERE article_id = ' . $cids;

            $this->_db->setQuery($query);

            if ($this->_db->query()) {
                $this->setError($this->_db->getErrorMsg());
                return false;
            }
        }
        return true;
    }
    function check_content_temp_edit($cid = array()) {
        $results = false;

        if (count($cid)) {
            JArrayHelper::toInteger($cid);
            $cids = implode(',', $cid);
            // echo $cids; die();
            $query = 'Select * FROM #__content_temp_edit WHERE content_temp_id = ' . $cids;

            $this->_db->setQuery($query);

            $this->_data = $this->_db->loadObject();
            //echo 'test'; die();           
            return (boolean) $this->_data;
        }

        return true;
    }

    function saveComment($data) {
        $row = & $this->getTable('articleflow_comment');
        //echo $row; die();
        //Dua cac truong trong form vao bang
        if (!$row->bind($data)) {

            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        //print_r($row);
        // Make sure the web link tale is valid
        if (!$row->check()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        // Store the web link table to the database
        if (!$row->store()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
        return true;
    }
     function getUserNameByCreated($id) {
        $db = JFactory::getDBO();
        $query = 'Select u.name  from #__users as u where u.id = ' . $id;
        $db->setQuery($query);

        $this->_user = $db->loadObject();
        return $this->_user;
    }

}

?>
